package com.ctrip.cap.performance;

import java.lang.reflect.Method;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Listeners;
import org.testng.xml.XmlTest;

import com.ctrip.cap.CapLogger;
import com.ctrip.cap.device.DeviceStoreFactory;
import com.ctrip.cap.device.DeviceStoreFactory.MobilePlatformType;
import com.ctrip.cap.internal.domain.Context;
import com.ctrip.cap.internal.suite.JobSetUpListener;
import com.ctrip.cap.internal.suite.ReporterSuiteListener;
import com.ctrip.cap.internal.suite.SuiteManListener;
import com.ctrip.cap.internal.util.FtpUtils;
import com.ctrip.cap.internal.util.HttpUtils;
import com.ctrip.cap.internal.util.ZipUtils;
import com.ctrip.cap.lanucher.AppiumConfig.DriverType;
import com.ctrip.cap.lanucher.Config;
import com.ctrip.cap.lanucher.Environment;
import com.ctrip.cap.reporter.CapReporter;

/**
 * Currentlly for
 * 
 * @author ltyao
 *
 */
@Listeners(value = { CapReporter.class, ReporterSuiteListener.class,
		JobSetUpListener.class })
public class BenchmarkTest {

	private static final Logger iLogger = LoggerFactory
			.getLogger(BenchmarkTest.class);

	private static final ThreadLocal<Context> contextHolder = new ThreadLocal<>();

	@BeforeSuite
	public void setUpSuite(ITestContext context) {

		CapLogger.setContext(contextHolder);

		String platform = context.getSuite().getParameter("platformName");
		
		DeviceStoreFactory.create("IOS".equalsIgnoreCase(platform)?MobilePlatformType.iOS:MobilePlatformType.Android);

		Config config = new Config();
		config.setDriverType(DriverType.Benchmark);
		config.setPlatformName(platform);
		context.getSuite().setAttribute(SuiteManListener.CAP_CONFIG,
				config);

		Map<String, String> ps = context.getSuite().getXmlSuite()
				.getParameters();
		String app = context.getSuite().getParameter("app");
		String envApp = Environment.getAppPath();
		
		if (StringUtils.isNotBlank(envApp)) {
			app = envApp;
		}
		
		if (StringUtils.isNotBlank(app) && app.toLowerCase().startsWith("ftp")) {
			app = FtpUtils.convertToHttpProdSite(app);
		}

		iLogger.info("app is " + app);
		
		if (StringUtils.isNotBlank(app) && app.toLowerCase().startsWith("http")) {

			app = HttpUtils.downloadApp(app);
		}
		
		if(StringUtils.endsWithIgnoreCase(app, ".zip")){

			app = ZipUtils.unzip(app);
			iLogger.info("upzip app is " + app);
		}
		
		
		ps.put("app", app);
		
		iLogger.info("put as testNG app parameter: " + app);
		
		context.getSuite().getXmlSuite().setParameters(ps);
	}

	@BeforeMethod(alwaysRun = true)
	public void setUpAPIContext(ITestContext testContext, Object[] parameters,
			Method method, XmlTest xmlTest) {

		iLogger.info("before {}", method.getName());
		Context context = new Context();
		context.setTestContext(testContext);
		context.setXmlTest(xmlTest);
		context.setMethod(method);
		contextHolder.set(context);
	}

	@AfterMethod(alwaysRun = true)
	public void destroyAPIContext(ITestContext testContext,
			ITestResult testResult, Method method) {

		contextHolder.remove();

	}

	@AfterSuite
	public void tearDownSuite() {
		DeviceStoreFactory.getDeviceStore().shutdownForcely();
	}

}
